package com.sunday.gateway.study.circuit.breaker;

import com.sunday.common.core.remoting.rest.response.RestResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Optional;

/**
 * @Description: demo程序
 */
@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class MyController {

    private final MyService myService;

    private final MyAnnotationService myAnnotationService;

    @GetMapping("/some")
    public Mono<RestResponse> someServiceMethod(ServerWebExchange exchange) {

        MultiValueMap<String, String> multiValueMap = exchange.getRequest().getQueryParams();
        multiValueMap.toSingleValueMap().forEach((k, v) -> log.info("请求参数 : {} = {}", k, v));

        long seconds = Optional.ofNullable(multiValueMap.getFirst("seconds"))
                .map(Long::parseLong)
                .orElse(0L);

//        return myService.process(seconds)
//                .doOnNext(result -> log.info("请求结束 : {}", result))
//                .doOnError(throwable -> log.info("{}", throwable.toString()))
//                .map(result -> RestResponse.ok(result));

        return myAnnotationService.process(seconds)
                .doOnNext(result -> log.info("请求结束 : {}", result))
                .doOnError(throwable -> log.info("{}", throwable.toString()))
                .map(result -> RestResponse.ok(result));

    }

}